package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.PaperTemplatesDao;
import com.xtedu.examproject.entity.Teacher;
import com.xtedu.examproject.entity.paperTemplates;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PaperTemplatesDapImpl implements PaperTemplatesDao {
    @Override
    public int insert(paperTemplates paperTemplates) throws Exception {
        String sql = "insert into paper_templates (template_name,description,create_time,is_default) values (?,?,?,?)";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            ps.setString(1, paperTemplates.getTemplateName());
            ps.setString(2, paperTemplates.getDescription());
            ps.setTimestamp(3, paperTemplates.getCreateTime());
            ps.setBoolean(4, paperTemplates.isDefault());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(paperTemplates paperTemplates) throws Exception {
        String sql = "update paper_templates set template_name=?,description=?,create_time=?,is_default=? where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            ps.setString(1, paperTemplates.getTemplateName());
            ps.setString(2, paperTemplates.getDescription());
            ps.setTimestamp(3, paperTemplates.getCreateTime());
            ps.setBoolean(4, paperTemplates.isDefault());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(paperTemplates paperTemplates) throws Exception {
        String sql = "delete from paper_templates where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            ps.setInt(1, paperTemplates.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public paperTemplates findById(int id) throws Exception {
        return null;
    }

    @Override
    public List<paperTemplates> findAll() throws Exception {
        String sql = "select * from paper_templates";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            ResultSet rs = ps.executeQuery();
            List<paperTemplates> TemplatesList = new ArrayList<>();
            while (rs.next()) {
                paperTemplates paperTemplates = new paperTemplates();
                paperTemplates.setId(rs.getInt("id"));
                paperTemplates.setTemplateName(rs.getString("template_name"));
                paperTemplates.setDescription(rs.getString("description"));
                paperTemplates.setCreateTime(rs.getTimestamp("create_time"));
                paperTemplates.setDefault(rs.getBoolean("is_default"));
                TemplatesList.add(paperTemplates);
            }
            return TemplatesList;
        }

    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    /**
     * 根据id删除模板
     * @param TemplateId
     * @return
     * @throws Exception
     */
    public boolean deleteTemplateById(int TemplateId) {
        String sql = "DELETE FROM teachers WHERE id = ?";

        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, TemplateId);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据ID查到的信息来修改模板信息
     * @param paperTemplates
     */
    public boolean updatePaperTemplates(paperTemplates paperTemplates) throws Exception {
        String sql = "UPDATE paper_templates SET template_name = ?,description = ?,isDefault = ? where id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)
        ) {

            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据ID来查询模板信息
     * @param TemplateId
     * @return
     */
    public paperTemplates findTemplateById(int TemplateId) throws Exception {
        String sql = "SELECT * FROM paper_templates WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, TemplateId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    paperTemplates paperTemplates = new paperTemplates();
                    paperTemplates.setId(rs.getInt("id"));
                    paperTemplates.setTemplateName(rs.getString("template_name"));
                    paperTemplates.setDescription(rs.getString("description"));
                    paperTemplates.setCreateTime(rs.getTimestamp("create_time"));
                    paperTemplates.setDefault(rs.getBoolean("is_default"));
                    return paperTemplates;
                }
            }
        }
        return null;
    }

    /**
     * 查询到所有的模板ID
     */
    public List<Integer> findAllTemplateId() throws Exception {
        String sql = "SELECT id FROM paper_templates";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            try (ResultSet rs = ps.executeQuery()) {
                List<Integer> TemplateIdList = new ArrayList<>();
                while (rs.next()) {
                    TemplateIdList.add(rs.getInt("id"));
                }
                return TemplateIdList;
            }
        }
    }
}
